
package com.tools.cleanmaster.ui.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.nineoldandroids.animation.ArgbEvaluator;
import com.tools.cleanmaster.CleanApplication;
import com.tools.cleanmaster.CleanConstant;
import com.tools.cleanmaster.R;

public class DynamicColorController {

    private ArgbEvaluator mColorEvaluator;
    private static final String TAG = "DynamicColorController";
    private DynamicColorListener mListener;
    private int mCurrentStartColor;
    private int mCurrentEndColor = Integer.MIN_VALUE;
    // 颜色动态设置开始时间
    private long mStartTime = 0;
    // 颜色动态总时间
    private static final int TIME_TOTAL = 1200;
    // 刷新颜色的间隔
    protected static final long TIME_DELAY = 20;

    private int mNewEndColor = Integer.MIN_VALUE;

    private static final int MSG_UPDATE = 0;
    public static final int TYPE_BG = 1;
    public static final int TYPE_SHADER = 2;
    public static final int TYPE_INNER_CIRCLE = 3;
    protected static final int  PROGRESS_STEP = 2;

    private static int COLOR_BG_1 = Integer.MIN_VALUE;
    private static int COLOR_BG_2;
    private static int COLOR_BG_3;
    private static int COLOR_BG_4;
    private static int COLOR_BG_5;

    private static int COLOR_SHADER_1;
    private static int COLOR_SHADER_2;
    private static int COLOR_SHADER_3;
    private static int COLOR_SHADER_4;
    private static int COLOR_SHADER_5;

    private static int COLOR_INNER_CIRCLE_1;
    private static int COLOR_INNER_CIRCLE_2;
    private static int COLOR_INNER_CIRCLE_3;
    private static int COLOR_INNER_CIRCLE_4;
    private static int COLOR_INNER_CIRCLE_5;

    @SuppressLint("HandlerLeak")
    private Handler mHander = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_UPDATE:
                    // 用mCurrentProgress判断来判断是否结束
                    long now = System.currentTimeMillis();
                    if (now < mStartTime + TIME_TOTAL) {
                        mListener.onColorChange((Integer) mColorEvaluator.evaluate((float) (now - mStartTime) / TIME_TOTAL, mCurrentStartColor, mCurrentEndColor));
                        mHander.sendEmptyMessageDelayed(MSG_UPDATE, TIME_DELAY);
                    } else {
                        // 结束本次设置
                        mListener.onColorChange(mCurrentEndColor);
                        mCurrentStartColor = mCurrentEndColor;
                        // 如果有mNewEndColor且不同的END,则开始新一轮；否则结束
                        if (mNewEndColor != Integer.MIN_VALUE) {
                            if (mNewEndColor == mCurrentEndColor) {
                                mNewEndColor = mCurrentEndColor = Integer.MIN_VALUE;
                            } else {
                                mCurrentEndColor = Integer.MIN_VALUE;
                                setColor(mNewEndColor);
                                mNewEndColor = Integer.MIN_VALUE;
                            }
                        } else {
                            mCurrentEndColor = Integer.MIN_VALUE;
                        }
                    }
                    break;

                default:
                    break;
            }
        }
    };

    public DynamicColorController(int startColor, DynamicColorListener listener) {
        mColorEvaluator = new ArgbEvaluator();
        mCurrentStartColor = startColor;
        mListener = listener;
    }

    private static void initColors() {
        Context context = CleanApplication.getInstance();
        COLOR_BG_5 = context.getResources().getColor(R.color.common_background_red);
        COLOR_SHADER_5 = context.getResources().getColor(R.color.shader_red);
        COLOR_INNER_CIRCLE_5 = context.getResources().getColor(R.color.inner_circle_red);
        COLOR_BG_4 = context.getResources().getColor(R.color.common_background_orange);
        COLOR_SHADER_4 = context.getResources().getColor(R.color.shader_orange);
        COLOR_INNER_CIRCLE_4 = context.getResources().getColor(R.color.inner_circle_orange);
        COLOR_BG_3 = context.getResources().getColor(R.color.common_background_yellow);
        COLOR_SHADER_3 = context.getResources().getColor(R.color.shader_yellow);
        COLOR_INNER_CIRCLE_3 = context.getResources().getColor(R.color.inner_circle_yellow);
        COLOR_BG_2 = context.getResources().getColor(R.color.common_background_green);
        COLOR_SHADER_2 = context.getResources().getColor(R.color.shader_green);
        COLOR_INNER_CIRCLE_2 = context.getResources().getColor(R.color.inner_circle_green);
        COLOR_BG_1 = context.getResources().getColor(R.color.common_background_blue);
        COLOR_SHADER_1 = context.getResources().getColor(R.color.shader_blue);
        COLOR_INNER_CIRCLE_1 = context.getResources().getColor(R.color.inner_circle_blue);
    }

    /**
     * 结束，还原状态，并将mCurrentStartColor设置为结束颜色
     */
    public void end() {
        mHander.removeMessages(MSG_UPDATE);
        if (mNewEndColor != Integer.MIN_VALUE) {
            mListener.onColorChange(mNewEndColor);
            mCurrentStartColor = mNewEndColor;
        } else if (mCurrentEndColor != Integer.MIN_VALUE) {
            mListener.onColorChange(mCurrentEndColor);
            mCurrentStartColor = mCurrentEndColor;
        }
        mCurrentEndColor = mNewEndColor = Integer.MIN_VALUE;
    }

    /**
     * 设置颜色，会动态设置
     * 
     * @param color
     */
    public void setColor(int color) {
        if (color != mCurrentStartColor) {
            // 判断是否在动态改变：如果不是，则开始；如果已经是：则只设置mNewEndColor
            if (mCurrentEndColor == Integer.MIN_VALUE) {
                mStartTime = System.currentTimeMillis();
                mCurrentEndColor = color;
                mNewEndColor = Integer.MIN_VALUE;
                mHander.sendEmptyMessage(MSG_UPDATE);
            } else {
                mNewEndColor = color;
            }
        }
    }

    /**
     * 将状态恢复为初始状态
     * @param startColor 初始的颜色
     */
    public void reset(int startColor) {
        mHander.removeMessages(MSG_UPDATE);
        mCurrentStartColor = startColor;
        mCurrentEndColor = mNewEndColor = Integer.MIN_VALUE;
    }

    /**
     * DynamicColor回调接口
     */
    public interface DynamicColorListener {
        void onColorChange(int color);
    }

    /**
     * size转化为color
     * 
     * @param context
     * @param size
     * @param type
     * @return
     */
    public static int getColorBySize(Context context, long size, int type) {
        if (COLOR_BG_1 == Integer.MIN_VALUE) {
            initColors();
        }
        if (size >= CleanConstant.TRASH_CLEAN_200M) {
            switch (type) {
                case TYPE_BG:
                    return COLOR_BG_5;
                case TYPE_SHADER:
                    return COLOR_SHADER_5;
                case TYPE_INNER_CIRCLE:
                    return COLOR_INNER_CIRCLE_5;
                default:
                    return 0;
            }
        } else if (size >= CleanConstant.TRASH_CLEAN_150M) {
            switch (type) {
                case TYPE_BG:
                    return COLOR_BG_4;
                case TYPE_SHADER:
                    return COLOR_SHADER_4;
                case TYPE_INNER_CIRCLE:
                    return COLOR_INNER_CIRCLE_4;
                default:
                    return 0;
            }
        } else if (size >= CleanConstant.TRASH_CLEAN_100M) {
            switch (type) {
                case TYPE_BG:
                    return COLOR_BG_3;
                case TYPE_SHADER:
                    return COLOR_SHADER_3;
                case TYPE_INNER_CIRCLE:
                    return COLOR_INNER_CIRCLE_3;
                default:
                    return 0;
            }
        } else if (size >= CleanConstant.TRASH_CLEAN_50M) {
            switch (type) {
                case TYPE_BG:
                    return COLOR_BG_2;
                case TYPE_SHADER:
                    return COLOR_SHADER_2;
                case TYPE_INNER_CIRCLE:
                    return COLOR_INNER_CIRCLE_2;
                default:
                    return 0;
            }
        } else {
            switch (type) {
                case TYPE_BG:
                    return COLOR_BG_1;
                case TYPE_SHADER:
                    return COLOR_SHADER_1;
                case TYPE_INNER_CIRCLE:
                    return COLOR_INNER_CIRCLE_1;
                default:
                    return 0;
            }
        }
    }
}
